##########################################################################################

library(data.table)
library(optparse)
library(dplyr)

##########################################################################################

option_list <- list(
    make_option(c("--sample_list_file"), type = "character"),
    make_option(c("--igc_cluster_file"), type = "character"),
    make_option(c("--dgc_cluster_file"), type = "character"),
    make_option(c("--smg_file"), type = "character"),
    make_option(c("--cgc_file"), type = "character"),
    make_option(c("--hll_pathway_file"), type = "character"),
    make_option(c("--hub_file"), type = "character"),
    make_option(c("--gtf_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    sample_list_file <- "~/20220915_gastric_multiple/rna_combine/analysis/config/tumor_normal.list"
    igc_cluster_file <- "~/20220915_gastric_multiple/rna_combine/analysis/images/mfuzz/mfuzz_plot_IGC.tsv"
    dgc_cluster_file <- "~/20220915_gastric_multiple/rna_combine/analysis/images/mfuzz/mfuzz_plot_DGC.tsv"
    diff_file <- "~/20220915_gastric_multiple/rna_combine/analysis/images/DiffGene/DiffGene.tsv"
    smg_file <- "~/20220915_gastric_multiple/rna_combine/analysis/public_ref/SMG_sort.list"
    cgc_file <- "~/20220915_gastric_multiple/rna_combine/analysis/public_ref/cancer_gene_census.list"
    hll_pathway_file <- "~/ref/Pathway/h.all.v7.5.1.symbols.txt"
    hub_file <- "~/20220915_gastric_multiple/rna_combine/analysis/images/wgcnv/wgcnv_HubModule.tsv"
    gtf_file <- "~/ref/GTF/gencode.v19.ensg_genename.txt"

    out_path <- "~/20220915_gastric_multiple/rna_combine/analysis/images/mfuzz"

}

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

sample_list_file <- opt$sample_list_file
igc_cluster_file <- opt$igc_cluster_file
dgc_cluster_file <- opt$dgc_cluster_file
out_path <- opt$out_path
smg_file <- opt$smg_file
cgc_file <- opt$cgc_file
hll_pathway_file <- opt$hll_pathway_file
hub_file <- opt$hub_file
gtf_file <- opt$gtf_file

dir.create(out_path , recursive = T)

##########################################################################################

info <- data.frame(fread(sample_list_file))
dat_igc_cluster <- data.frame(fread(igc_cluster_file))
dat_dgc_cluster <- data.frame(fread(dgc_cluster_file))
dat_smg <- data.frame(fread(smg_file , header = F))
dat_cgc <- data.frame(fread(cgc_file , header = T))
dat_hll <- data.frame(fread(hll_pathway_file , header = T))
dat_hub <- data.frame(fread(hub_file , header = T))
dat_gtf <- data.frame(fread(gtf_file , header = F))
colnames(dat_gtf) <- c("gene_id" , "Hugo_Symbol")

##########################################################################################

class_type <- c( "Normal" , "IM" , "IGC" , "DGC")

dat_hll_gene <- dat_hll %>%
group_by(gene) %>%
summarize( pathway_num = length(unique(pathway)) , pathway=paste0(pathway , collapse = ",") )

##########################################################################################
annotationGene <- function( dat_input = dat_input , out_name = out_name ){

    dat_input <- merge( dat_input , dat_gtf , bu = "gene_id" )
    dat_input$SMG <- ifelse( dat_input$Hugo_Symbol %in% dat_smg$V1 , "TRUE" , "FALSE" )
    dat_input <- merge( dat_input , dat_cgc[-2] , by.x = "Hugo_Symbol" , by.y = "Gene.Symbol" , all.x = T )
    dat_input <- merge( dat_input , dat_hll_gene , by.x = "Hugo_Symbol" , by.y = "gene" , all.x = T )
    dat_input <- merge( dat_input , dat_hub , by = "gene_id" , all.x = T )

    write.table( dat_input , out_name , row.names = F , quote = F , sep = "\t" )

}

dat_input <- dat_igc_cluster
out_name <- paste0(out_path , "/mfuzz_plot_IGC.annotation.tsv")
annotationGene( dat_input = dat_input , out_name = out_name )

dat_input <- dat_dgc_cluster
out_name <- paste0(out_path , "/mfuzz_plot_DGC.annotation.tsv")
annotationGene( dat_input = dat_input , out_name = out_name )